package com.zwj.interview.链表;

//模板代码
@Deprecated
public class 哨兵节点操作链表 {

    /**
     * 哨兵节点增加节点
     *
     * @param head
     * @param value
     * @return
     */
    public ListNode append(ListNode head, int value) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode newNode = new ListNode(value);
        ListNode node = dummy;
        //走到最后一个节点，然后接上新的节点
        while (node.next != null) {
            node = node.next;
        }
        node.next = newNode;
        return dummy.next;
    }

    /**
     * 使用哨兵节点删除节点
     *
     * @param head
     * @param value
     * @return
     */
    public ListNode delete(ListNode head, int value) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode node = dummy;
        while (node.next != null) {
            if (node.next.val == value) {
                node.next = node.next.next;
                break;
            }
            node = node.next;
        }
        return dummy.next;
    }



}
